﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace Groovy_IM
{
    public partial class ManageAccounts : PhoneApplicationPage
    {
        bool ShowedAccounts = false;

        bool loaded = false;

        public ManageAccounts()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(ManageAccounts_Loaded);

            loaded = false;
        }

        void ManageAccounts_Loaded(object sender, RoutedEventArgs e)
        {
            if (ShowedAccounts == false)
            {
                if (App.Accounts.Items.Count < 1)
                    NavigationService.Navigate(new Uri("/AddAccounts.xaml", UriKind.Relative));

                ShowedAccounts = true;
            }

            pivotControl.Items.Clear();

            loaded = false;

            for (int i = 0; i < App.Accounts.Items.Count; i++)
            {
                PivotItem pvItem = new PivotItem();

                pvItem.Header = App.Accounts.Items[i].Username;

                ListBox lb = new ListBox();

                lb.Margin = new Thickness(0, 0, -12, 0);

                TextBlock tb = new TextBlock();

                tb.Text = App.Accounts.Items[i].Description;

                tb.FontSize = 32;

                tb.TextAlignment = TextAlignment.Center;

                tb.Width = 455;

                ToggleSwitch signOn = new ToggleSwitch();

                signOn.Tag = i.ToString();

                signOn.Header = "Signed On";

                signOn.IsChecked = App.Accounts.Items[i].SignedOn;

                signOn.Checked += new EventHandler<RoutedEventArgs>(signOn_Checked);

                signOn.Unchecked += new EventHandler<RoutedEventArgs>(signOn_Unchecked);

                signOn.Width = 455;

                ToggleSwitch invisLogin = new ToggleSwitch();

                invisLogin.Tag = i.ToString();

                invisLogin.Header = "Invisible Login";

                invisLogin.IsChecked = App.Accounts.Items[i].InvisibileLogin;

                invisLogin.Checked += new EventHandler<RoutedEventArgs>(invisLogin_Checked);

                invisLogin.Unchecked += new EventHandler<RoutedEventArgs>(invisLogin_Unchecked);

                invisLogin.Width = 455;

                ToggleSwitch wpLogin = new ToggleSwitch();

                wpLogin.Tag = i.ToString();

                wpLogin.Header = "Show as Windows Phone 7";

                wpLogin.IsChecked = App.Accounts.Items[i].MobileLogin;

                wpLogin.Checked += new EventHandler<RoutedEventArgs>(wpLogin_Checked);

                wpLogin.Unchecked += new EventHandler<RoutedEventArgs>(wpLogin_Unchecked);

                wpLogin.Width = 455;

                TextBlock tb2 = new TextBlock();

                tb2.Text = "Status";

                tb2.FontSize = 32;

                tb2.TextAlignment = TextAlignment.Center;

                tb2.Width = 455;

                TextBox statusBox = new TextBox();

                statusBox.Text = "On Windows Phone 7...";

                statusBox.TextAlignment = TextAlignment.Center;

                statusBox.Width = 455;

                statusBox.GotFocus += new RoutedEventHandler(statusBox_GotFocus);

                statusBox.LostFocus += new RoutedEventHandler(statusBox_LostFocus);

                lb.Items.Add(tb);

                lb.Items.Add(signOn);

                lb.Items.Add(invisLogin);

                lb.Items.Add(wpLogin);

                lb.Items.Add(tb2);

                lb.Items.Add(statusBox);

                pvItem.Content = lb;

                pivotControl.Items.Add(pvItem);
            }

            loaded = true;
        }

        void statusBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox status = (sender as TextBox);

            if (status.Text.Replace(" ", "").ToLower() == "")
            {
                status.Text = "On Windows Phone 7...";
            }

            App.m_Proto[pivotControl.SelectedIndex].SetStatus(status.Text);
        }

        void statusBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox status = (sender as TextBox);

            if (status.Text == "On Windows Phone 7...")
            {
                status.Text = "";
            }
        }
        
        void invisLogin_Unchecked(object sender, RoutedEventArgs e)
        {
            string rTag = (sender as ToggleSwitch).Tag.ToString();

            int Tag = Convert.ToInt32(rTag);

            App.Accounts.Items[Tag].InvisibileLogin = false;
        }

        void invisLogin_Checked(object sender, RoutedEventArgs e)
        {
            string rTag = (sender as ToggleSwitch).Tag.ToString();

            int Tag = Convert.ToInt32(rTag);

            App.Accounts.Items[Tag].InvisibileLogin = true;
        }

        void signOn_Unchecked(object sender, RoutedEventArgs e)
        {
            if (loaded == true)
            {
                string rTag = (sender as ToggleSwitch).Tag.ToString();

                int Tag = Convert.ToInt32(rTag);

                App.Accounts.Items[Tag].SignedOn = false;

                App.m_Proto[Tag].SignOff();
            }
        }

        void signOn_Checked(object sender, RoutedEventArgs e)
        {
            if (loaded == true)
            {
                string rTag = (sender as ToggleSwitch).Tag.ToString();

                int Tag = Convert.ToInt32(rTag);

                App.Accounts.Items[Tag].SignedOn = true;

                App.m_Proto[Tag].SignOn(Tag);
            }
        }

        void wpLogin_Unchecked(object sender, RoutedEventArgs e)
        {
            string rTag = (sender as ToggleSwitch).Tag.ToString();

            int Tag = Convert.ToInt32(rTag);

            App.Accounts.Items[Tag].MobileLogin = false;
        }

        void wpLogin_Checked(object sender, RoutedEventArgs e)
        {
            string rTag = (sender as ToggleSwitch).Tag.ToString();

            int Tag = Convert.ToInt32(rTag);

            App.Accounts.Items[Tag].MobileLogin = true;
        }

        private void addAcc_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddAccounts.xaml", UriKind.Relative));
        }

        private void remAcc_Click(object sender, EventArgs e)
        {
            if (pivotControl.SelectedIndex < 0)
                return;

            App.m_Proto[pivotControl.SelectedIndex].SignOff();
            App.Accounts.RemoveItem(pivotControl.SelectedIndex);
            pivotControl.Items.RemoveAt(pivotControl.SelectedIndex);
        }
    }
}